package cn.itcast.goods.admin.book.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.book.domain.Book;
import cn.itcast.goods.book.service.BookService;
import cn.itcast.goods.category.doamin.Category;
import cn.itcast.goods.category.service.CategoryService;
import cn.itcast.goods.pager.PageBean;
import cn.itcast.servlet.BaseServlet;

public class AdminBookServlet extends BaseServlet {

	private BookService bookService = new BookService();
	private CategoryService categoryService = new CategoryService();
	/**
	 * 获取当前页码
	 * @param request
	 * @return
	 */
	private int getPageCode(HttpServletRequest request)
	{
		String pageCode = request.getParameter("pc");
		if(pageCode == null)
		{
			return 1;
		}
		try
		{
			return Integer.parseInt(pageCode);
		}
        catch(Exception e)
        {
        	return 1;
        }
	}
	
	/**
	 * 获取当前的url,但去除pc参数
	 * @param request
	 * @return
	 */
    private String getUrl(HttpServletRequest request)
    {
    	String url = request.getRequestURI()+"?"+request.getQueryString();
    	int fromIndex = url.lastIndexOf("&pc");
    	if(fromIndex == -1)
    	{
    		return url;
    	}
    	int toIndex = url.indexOf("&",fromIndex+1);
    	if(toIndex == -1)
    	{
    		return url.substring(0,fromIndex);
    	}
    	return url.substring(0, fromIndex)+url.substring(toIndex); 
    }
	/**
	 * 根据分类查找图书
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pc = getPageCode(request);
		String cid = request.getParameter("cid");
        PageBean<Book>  pb = bookService.findByCategory(cid, pc);
        String url = getUrl(request);
        pb.setUrl(url);
        request.setAttribute("pb", pb);
        return "/adminjsps/admin/book/list.jsp";
	}
	
	/**
	 * 根据书名查找图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByBname(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pc = getPageCode(request);
		String bname = request.getParameter("bname");
		PageBean<Book> pb = bookService.findByBname(bname, pc);
		String url= getUrl(request);
		pb.setUrl(url);
		request.setAttribute("pb", pb);
		return "/adminjsps/admin/book/list.jsp";
	}
	
	/**
	 * 按作者查找图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByAuthor(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pc = getPageCode(request);
		String author = request.getParameter("author");
		PageBean<Book> pb = bookService.findByAuthor(author, pc);
		String url = getUrl(request);
	    pb.setUrl(url);
	    request.setAttribute("pb", pb);
		return "/adminjsps/admin/book/list.jsp";
	}
	
	/**
	 * 根据出版社查找图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByPress(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pc = getPageCode(request);
		String press = request.getParameter("press");
		PageBean<Book> pb=bookService.findByPress(press, pc);
		String url=getUrl(request);
		pb.setUrl(url);
		request.setAttribute("pb",pb);
		return "/adminjsps/admin/book/list.jsp";
	}
	
	/**
	 * 根据组合条件查找图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCombination(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pc = getPageCode(request);
		Book book = CommonUtils.toBean(request.getParameterMap(), Book.class);
		PageBean<Book> pb = bookService.findByCombination(book, pc);
		String url = getUrl(request);
	    pb.setUrl(url);
		return "/adminjsps/admin/book/list.jsp";
	}
	
	
	/**
	 * 加载图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String load(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		String bid = request.getParameter("bid");
		Book book = bookService.load(bid);
		request.setAttribute("book", book);
		request.setAttribute("parents", categoryService.findByParents());
		String pid = book.getCategory().getPid();
		request.setAttribute("children", categoryService.findChildren(pid));
		return "/adminjsps/admin/book/desc.jsp";
	}
	
	/**
	 * 编辑图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Book book = CommonUtils.toBean(request.getParameterMap(), Book.class);
		Category category = CommonUtils.toBean(request.getParameterMap(), Category.class);
		Category parent = new Category();
		parent.setCid(request.getParameter("pid"));
        category.setParent(parent);
        book.setCategory(category);
		try
		{
           bookService.edit(book);
		}catch(Exception e)
		{
			request.setAttribute("msg", "编辑图书失败，请重新操作!"+"<br/>"+e.getMessage());
			return "/adminjsps/msg.jsp";
		}
		request.setAttribute("msg", "编辑图书成功！");
		return "/adminjsps/msg.jsp";
	}
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bid = request.getParameter("bid");
		try
		{
			bookService.delete(bid);
		}
		catch(Exception e)
		{
			request.setAttribute("msg", "删除图书失败"+"<br/>"+e.getMessage());
			return "/adminjsps/msg.jsp";
		}
		request.setAttribute("msg", "删除图书成功！");
		return "/adminjsps/msg.jsp";
	}
	
	/**
	 * 添加图书，第一步
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addPre(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("parents", categoryService.findByParents());
		return "f:/adminjsps/admin/book/add.jsp";
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	
	}

}
